# coding: utf-8

# -------------------------------------------------------------------------------
# Name:         ext_method.py
# Description:  扩展方法视图
# Author:       XiangjunZhao
# EMAIL:        2419352654@qq.com
# Date:         2020/12/13 下午6:36
# -------------------------------------------------------------------------------
import os

from django.conf import settings
from rest_framework import status
from rest_framework.mixins import UpdateModelMixin, DestroyModelMixin, CreateModelMixin, ListModelMixin
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet, GenericViewSet

from QAPlatform.mixins import CustomCreateModelMixin
from apps.HttpAutoTestService.filters import ExtMethodFilter, ExtMethodOnlineFilter
from apps.HttpAutoTestService.models import ExtMethod, ExtMethodOnline
from apps.HttpAutoTestService.serializers import ExtMethodListSerializer, ExtMethodSerializer, \
    ExtMethodNameListSerializer, ExtMethodOnlineSerializer

__all__ = ['ExtMethodNameListViewSet', 'ExtMethodListViewSet', 'ExtMethodViewSet', 'ExtMethodOnlineViewSet']


class ExtMethodNameListViewSet(ReadOnlyModelViewSet):
    """
    扩展方法名称列表
    """
    queryset = ExtMethod.objects.filter(is_deleted=False)
    serializer_class = ExtMethodNameListSerializer
    filter_class = ExtMethodFilter
    pagination_class = None


class ExtMethodListViewSet(ReadOnlyModelViewSet):
    """
    扩展方法列表
    """
    queryset = ExtMethod.objects.filter(is_deleted=False)
    serializer_class = ExtMethodListSerializer
    filter_class = ExtMethodFilter


class ExtMethodViewSet(CustomCreateModelMixin, UpdateModelMixin, DestroyModelMixin, GenericViewSet):
    """
    扩展方法创建、更新
    """
    queryset = ExtMethod.objects.filter(is_deleted=False)
    serializer_class = ExtMethodSerializer

    def destroy(self, request, *args, **kwargs):
        id = kwargs.get('pk')
        ExtMethod.objects.filter(id=id).update(is_deleted=True)
        return Response(status=status.HTTP_204_NO_CONTENT)


class ExtMethodOnlineViewSet(CreateModelMixin, UpdateModelMixin, ListModelMixin, GenericViewSet):
    queryset = ExtMethodOnline.objects.filter(is_deleted=False)
    serializer_class = ExtMethodOnlineSerializer
    filter_class = ExtMethodOnlineFilter
    pagination_class = None

    @staticmethod
    def write_file(ext_method_online):
        base_dir = settings.BASE_DIR
        filename = ext_method_online.get('name')
        filepath = ext_method_online.get('filepath', '').replace('/', os.sep)
        content = ext_method_online.get('content', '').replace('\r\n', '\n')
        # 文件绝对路径
        file_abspath = os.path.join(base_dir, filepath, filename)

        with open(file=file_abspath, mode='w+', encoding='utf-8') as fp:
            fp.write(content)

    def perform_create(self, serializer):
        serializer.save()
        self.write_file(ext_method_online=serializer.data)

    def perform_update(self, serializer):
        serializer.save()
        self.write_file(ext_method_online=serializer.data)
